package com.gitee.wsl.transform.escape.sample

import com.gitee.wsl.transform.escape.Escape
import com.gitee.wsl.transform.escape.EscapeString
import com.gitee.wsl.transform.escape.escape

fun String.safeXmlComment(): String {
    var comment = this
    if (comment.isEmpty()) return comment

    comment = comment.replace("--", "- - ")
    return comment
}

object XmlEscape: EscapeString(){
    val encodeXml11Map = listOf(
        "\u0000" to "",
        "\u000b" to "&#11;",
        "\u000c" to "&#12;",
        "\ufffe" to "",
        "\uffff" to "",
    )

    override val warpEntrys = listOf(
//			{"'" to "&apos;",// " - single-quote
        "\"" to "&quot;", // " - double-quote
        "&"  to "&amp;", // & - ampersand
        "<"  to "&lt;", // < - less-than
        ">"  to "&gt;" // > - greater-than
    ) + encodeXml11Map

    fun valueNeedEscape(str: String): Boolean {
        var i = 0
        val n = str.length
        val keys = warpEntrys.map { it.first }
        while (i < n) {
            if (keys.indexOf(str[i].toString()) >= 0) return true
            i++
        }
        return false
    }
}

val Escape.Companion.xml get() = XmlEscape

val String.escapeXml get() = escape(Escape.xml)